<?php

declare(strict_types=1);

namespace app\system\controller;

use app\admin\BaseAdminController;
use think\facade\View;
use app\system\model\Post as PostModel;
use think\facade\Request;
use app\admin\model\Data;
use app\system\model\LogOperate as Log;

class Post extends BaseAdminController
{
    /**
     * 岗位管理页面
     */
    public function index()
    {
        View::assign('title', '岗位管理/系统管理');
        return View::fetch();
    }

    /**
     * 导出岗位信息
     */
    public function export()
    {
        [$obj,] = $this->_();
        $list = $obj->order('id', 'desc')->select();
        $data = [];
        foreach ($list as $value) {
            $data[] = [
                '岗位编码' => $value['code'],
                '岗位名称' => $value['name'],
                '状态' => $value['status'] ? '停用' : '正常',
                '更新时间' => $value['update_time'],
                '更新人' => $value['update_by'],
            ];
        }
        $url = Data::export('岗位数据', $data);
        $get = Request::get();
        $get['excel'] = explode('=', $url)[1];
        Log::add('导出岗位数据', $get);
        return json(['url' => $url]);
    }

    /**
     * 查询岗位信息
     */
    private function _()
    {
        $code = input('code');
        $name = input('name');
        $status = input('status/d');
        $obj = new PostModel();
        if ($code) {
            $obj = $obj->where('code', $code);
        }
        if ($name) {
            $obj = $obj->where('name', $name);
        }
        if ($status) {
            $obj = $obj->where('status', $status - 1);
        }
        $total = $obj->count();
        return [$obj, $total];
    }

    /**
     * 读取岗位信息列表
     */
    public function data()
    {
        $offset = input('offset/d');
        $limit = input('limit/d');
        [$obj, $total] = $this->_();
        $list = $obj->order(['sort' => 'asc', 'id' => 'desc'])->limit($offset, $limit)->select();
        return json([
            'total' => $total,
            'rows' => $list,
        ]);
    }

    /**
     * 保存岗位信息
     */
    public function save()
    {
        $id = input('id/d', 0);
        $name = input('name');
        $code = input('code');
        $sort = input('sort/d', 0);
        $remark = input('remark');
        if ($id) {
            $title = '编辑岗位';
            $obj = PostModel::find($id);
        } else {
            $title = '新增岗位';
            $obj = new PostModel;
        }
        $obj->name = $name;
        $obj->code = $code;
        $obj->sort = $sort;
        $obj->remark = $remark;
        $obj->update_by = USERNAME;
        $obj->save();
        Log::add($title, [
            'ID' => $obj->id,
            '岗位编码' => $code,
            '岗位名称' => $name,
            '显示顺序' => $sort,
            '备注' => $remark,
        ]);
        return json(['msg' => '']);
    }

    /**
     * 岗位状态变更
     */
    public function status()
    {
        $id = input('id/d', 0);
        $obj = PostModel::find($id);
        $status = $obj->status ? '正常' : '停用';
        $obj->status = $obj->status ? 0 : 1;
        $obj->update_by = USERNAME;
        $obj->save();
        Log::add('岗位状态', [
            'ID' => $id,
            '岗位名称' => $obj->name,
            '状态' => $status,
        ]);
        return json(['msg' => '']);
    }

    /**
     * 删除岗位信息
     */
    public function remove()
    {
        $id = input('id/d', 0);
        $obj = PostModel::find($id);
        $obj->delete();
        Log::add('删除岗位', [
            'ID' => $id,
            '岗位编码' => $obj->code,
            '岗位名称' => $obj->name,
            '显示顺序' => $obj->sort,
            '备注' => $obj->remark,
        ]);
        return json(['msg' => '']);
    }

    /**
     * 查看岗位信息
     */
    public function view()
    {
        $id = input('id/d', 0);
        $obj = PostModel::find($id);
        return json($obj);
    }
}
